import os
import shutil
import common.gyblog as log

LOG = log.get_logger()
PATH = '\\\\lcnas\\xishu16t2020_1\choosefc2\\'
# PATH = r'\\lcnas\xishu16T_2022\新建文件夹 (2)\\'
# PATH = '\\\\lcnas\\xishu16t2020_1\qb_download\Ed Mosaic 不带番号或已下架整理合集02\\'

'''
将上一层文件夹删除
排除：00incomplete文件夹
'''


def delete_path(path):
    root_dirs = os.listdir(path)
    print('处理的文件夹数量：' + str(len(root_dirs)))
    LOG.info('处理的文件夹数量：' + str(len(root_dirs)))
    for dir in root_dirs:
        if dir == '00incomplete':
            continue
        else:
            try:
                dirs = os.listdir(path + dir)
                for d in dirs:
                    old_path = path + dir + '\\' + d
                    new_path = path + d
                    shutil.move(old_path, new_path)
                    print('移动文件：{},{}'.format(old_path, new_path))
                    LOG.info('移动文件：{},{}'.format(old_path, new_path))
            except shutil.Error as e:
                print(repr(e))
                LOG.error(repr(e))



# 删除空目录
def delete_kongdir(path):
    root_dirs = os.listdir(path)
    count = 0
    for dir in root_dirs:
        if os.path.isdir(path + dir):
            # print(dir + "是目录")
            num_file = os.listdir(path + dir + '\\')
            print(dir,len(num_file))
            if len(num_file) == 0:
                os.rmdir(path + dir)
                count = count + 1
                LOG.info('删除空文件夹：' + path + dir)
                print('删除空文件夹：' + path + dir)
            else:
                continue
        else:
            print(dir + "是文件")
    LOG.info('删除空文件夹完成')
    print('删除空文件夹完成')


if __name__ == '__main__':
    delete_path(PATH)
    delete_kongdir(PATH)
